//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System.Windows.Forms;
using GuidanceExplorer.Model;

namespace GuidanceExplorer.Controls
{
    /// <summary>
    /// Represents the root MyViews node in the tree.
    /// </summary>
    [MenuItemAttribute(510, "context\\New Folder", GuidanceExplorer.Commands.NewFolder, Keys.None)]
    [MenuItemAttribute(520, "context\\New View", GuidanceExplorer.Commands.NewView, Keys.None)]
    //[MenuItemAttribute(530, "context\\New Search View", GuidanceExplorer.Commands.NewSearchView, Keys.None)]
    [MenuItemAttribute(540, "context\\-", null, Keys.None)]
    [MenuItemAttribute(560, "context\\Export as Library...", GuidanceExplorer.Commands.ExportMyViewsAsLibrary, Keys.None)]
    
    //[MenuItemAttribute(561, "context\\Save View as...", GuidanceExplorer.Commands.SaveMyViewsAs, Keys.None)]
    public class MyViewsNode : ModelNode<UserViews>
    {
        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static MyViewsNode New()
        {
            return new MyViewsNode();
        }

        /// <summary>
        /// Default private constructor.
        /// </summary>
        private MyViewsNode() : base("My Views", 2, 2) { }

        /// <summary>
        /// Whether the node is editable.
        /// </summary>
        public override bool CanDrop(IModelItem modelNode)
        {
            if (modelNode == null)
            {
                return false;
            }

            ViewNode viewNode = modelNode as ViewNode;
            FolderNode folderNode = modelNode as FolderNode;
            SearchViewNode searchNode = modelNode as SearchViewNode;
			ChecklistNode checkListNode = modelNode as ChecklistNode;

			MenuItemUnderSolutionNodeCondition isUnderSolutionNode = new MenuItemUnderSolutionNodeCondition();

            if (folderNode != null)
            {
				if (isUnderSolutionNode.ShowMenuItem(null,folderNode)) return false;
                return true;
            }
            else if (viewNode != null)
            {
                /*
                 * Test for folders:
                 *  The root folders can't be droped in the library.
                 */

				if (isUnderSolutionNode.ShowMenuItem(null, viewNode))
				{
					return false;
				}

                //Root views cant be dragged
                if (viewNode.ModelInstance.RootView)
                {
                    return false;
                }

                //TODO: avoid droping into the same place.
                //if (Object.ReferenceEquals(viewNode.ModelInstance.Container, ModelInstance))
                //{
                //    return false;
                //}

                return true;
            }
            else if (searchNode != null)
            {
                //Root views cant be dragged
                if (searchNode.ModelInstance.RootView)
                {
                    return false;
                }
                return true;
            }
			else if (checkListNode != null)
			{
				if (isUnderSolutionNode.ShowMenuItem(null, checkListNode))
				{
					return false;
				}
				
				return true;
			}
			else
			{
				//Can't drop this model node.
			}
            return false;
        }
		
        public override bool IsEditable
        {
            get { return false; }
        }
    }
}

